package com.fieldchain.ex.fa100.ui.personal.activity;

import android.graphics.Bitmap;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.access.Constant;
import com.fieldchain.ex.fa100.base.MyBaseActivity;
import com.fieldchain.ex.fa100.bean.User;
import java.util.HashMap;
import com.fieldchain.ex.fa100.utils.common.VerticalUtils;
import com.fieldchain.ex.fa100.utils.net.access.HttpRequest;
import com.fieldchain.ex.fa100.utils.net.common.EmailCode;
import com.fieldchain.ex.fa100.utils.net.common.SmsCode;
import com.fieldchain.ex.fa100.utils.net.modle.ImageRequest;
import com.fieldchain.ex.fa100.utils.net.modle.StringRequest;
import com.fieldchain.ex.fa100.utils.screen.MyToast;
import com.fieldchain.ex.fa100.utils.share.ShareManager;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/15
 */
public class SetTradPwdActivity extends MyBaseActivity implements View.OnClickListener{
    private RelativeLayout reaBack;
    private TextView tvCenter;
    private TextView tvNametype;
    private TextView tvName;
    private EditText etImgCode;
    private ImageView ivImgcode;
    private EditText etSmsCode;
    private TextView tvSms;
    private EditText etPwd;
    private EditText etRpwd;
    private TextView tvSure;


    private String name;
    private User user;
    private boolean isEmail = false;
    @Override
    public void findView() {
        reaBack = findViewById(R.id.rea_back);
        tvCenter = findViewById(R.id.tv_center);
        tvNametype = findViewById(R.id.tv_nametype);
        tvName = findViewById(R.id.tv_name);
        etImgCode = findViewById(R.id.et_imgCode);
        ivImgcode = findViewById(R.id.iv_imgcode);
        etSmsCode = findViewById(R.id.et_smsCode);
        tvSms = findViewById(R.id.tv_sms);
        etPwd = findViewById(R.id.et_pwd);
        etRpwd = findViewById(R.id.et_rpwd);
        tvSure = findViewById(R.id.tv_sure);
    }

    @Override
    public void setValue() {
        getImage();
        user = ShareManager.getInstance().getUser();
        if(user==null){
            loginShow();
        }else{
            if (user.getPhone() != null && user.getPhone().length() > 0) {
                name = user.getPhone();
                isEmail = false;
            } else {
                tvNametype.setText("邮箱");
                name = user.getEmail();
                isEmail = true;
            }
            tvName.setText(name);
            tvCenter.setText("设置交易密码");
            reaBack.setOnClickListener(this);
            tvSure.setOnClickListener(this);
            ivImgcode.setOnClickListener(this);
            tvSms.setOnClickListener(this);
        }

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        VerticalUtils.getInstance().cancle();
    }
    @Override
    public int getLayout() {
        return R.layout.activity_settradepwd;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_imgcode:
                getImage();
                break;
            case R.id.tv_sms:
                VerticalUtils.getInstance().start(tvSms);
                if (!isEmail) {
                    SmsCode.getInstance().getSms(tvName.getText().toString(), etImgCode.getText().toString());
                } else {
                    EmailCode.getInstance().getEmail(tvName.getText().toString(), etImgCode.getText().toString());
                }
                break;
            case R.id.tv_sure:
                change();
                break;
            case R.id.rea_back:
                finish();
                break;
        }
    }

    public void getImage() {
        HttpRequest.getInstance().getImage(Constant.image,new ImageRequest() {
            @Override
            public void imgReceive(Bitmap bit) {
                ivImgcode.setImageBitmap(bit);
            }
        });
    }
    public void change() {
        String pws=etPwd.getText().toString();
        String rpws=etRpwd.getText().toString();
        String smsCode=etSmsCode.getText().toString();

        int length=pws.length();
        if(!(length>=6&&length<=20)){
            MyToast.show("密码格式为6-20位数字字母字符组合");
            return;
        }
        if(!pws.equals(rpws)){
            MyToast.show("两次输入不一致");
            return;
        }

        HashMap map=new HashMap();
        map.put("tpw",pws);
        map.put("smsCode",smsCode);
        HttpRequest.getInstance().postRequest(map, Constant.setTradPwd,new StringRequest() {
            @Override
            public void onSucess(String str, int code, String msg) {
                MyToast.show(msg);
                if(code==200){
                    finish();
                }
            }

            @Override
            public void onFaild(Exception e) {

            }
        });
    }
}
